﻿using System.Data;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public sealed class BasicTableInfoHelper
    {
        public static DataTable GetBasicInfo(IDictionary<string, ColumnInfo> columnList)
        {
            DataTable dataTable = BuildDataTable();
            foreach (KeyValuePair<string, ColumnInfo> column in columnList)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["Ordinal"] = column.Value.Ordinal;
                dataRow["Name"] = column.Value.Name.Name;
                dataRow["DataType"] = column.Value.DataType;
                dataRow["Length"] = column.Value.MaxLength;
                dataRow["PrimaryKey"] = column.Value.IsPrimaryKey;
                dataRow["AutoIncrement"] = column.Value.AutoIncrement;
                dataRow["AllowNull"] = column.Value.AllowDBNull;
                dataRow["DefaultValue"] = column.Value.DefaultValue;
                dataRow["Alias"] = column.Value.Name.Alias;
                dataRow["Description"] = column.Value.Description;
                dataTable.Rows.Add(dataRow);
            }
            return dataTable;
        }

        private static string BooleanToString(bool parameter)
        {
            string result = "N";
            if (parameter)
            {
                result = "Y";
            }
            return result;
        }

        private static DataTable BuildDataTable()
        {
            DataTable dataTable = new DataTable("ParentTable");
            DataColumn dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.Int32");
            dataColumn.ColumnName = "Ordinal";
            dataColumn.Caption = "编号";
            dataColumn.ReadOnly = true;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "Name";
            dataColumn.Caption = "名称";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "DataType";
            dataColumn.Caption = "数据类型";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "Length";
            dataColumn.Caption = "长度";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.Boolean");
            dataColumn.ColumnName = "PrimaryKey";
            dataColumn.Caption = "主键";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.Boolean");
            dataColumn.ColumnName = "AutoIncrement";
            dataColumn.Caption = "自增";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.Boolean");
            dataColumn.ColumnName = "AllowNull";
            dataColumn.Caption = "可空";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "DefaultValue";
            dataColumn.Caption = "默认值";
            dataColumn.ReadOnly = false;
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "Alias";
            dataColumn.Caption = "别名";
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn();
            dataColumn.DataType = Type.GetType("System.String");
            dataColumn.ColumnName = "Description";
            dataColumn.Caption = "字段描述";
            dataTable.Columns.Add(dataColumn);
            dataTable.PrimaryKey = new DataColumn[1] { dataTable.Columns["Ordinal"] };
            return dataTable;
        }
    }
}
